package com.lg.erp.controller.zhangchaofu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhangchaofu.CTIoWarehouse;
import com.lg.erp.entity.zhangchaofu.CTIomWarehouse;
import com.lg.erp.service.zhangchaofu.ICTIoWarehouseService;
import com.lg.erp.service.zhangchaofu.ICTIomWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class CTIoWarehouseController {

    @Autowired
    private ICTIoWarehouseService ioWarehouseService;
    @Autowired
    private ICTIomWarehouseService iomWarehouseService;

    @GetMapping("/ioWarehouses")
    public JsonResult getAllIoWarehouse(
            @RequestParam(value = "current",required = false,defaultValue = "1") Long current){
        Page<CTIoWarehouse> page = new Page<>(current,10);
        IPage<CTIoWarehouse> iPage = ioWarehouseService.page(page,null);
        return new JsonResult(1,"ok",iPage);
    }

    @GetMapping("/ioWarehouseSub")
    public JsonResult getOne(String ioNo,String batch){
        if (ioNo.equals("null") || batch.equals("null")){
            List<CTIoWarehouse> list = ioWarehouseService.list();
            return new JsonResult(1,"ok",list);
        }else {
            List<CTIoWarehouse> list = ioWarehouseService.list(new QueryWrapper<CTIoWarehouse>().lambda()
                    .eq(CTIoWarehouse::getIoNo, ioNo)
                    .eq(CTIoWarehouse::getBatch, batch));
            return new JsonResult(1,"ok",list);
        }
    }

    @PostMapping("/ioWarehouses")
    public JsonResult addIom(CTIoWarehouse ioWarehouse) {
        List<CTIoWarehouse> list2 = ioWarehouseService.findIo(ioWarehouse.getPName(),ioWarehouse.getBatch(),ioWarehouse.getWName());
        if (!list2.isEmpty()){
            ioWarehouse.setCompanyId("1");
            boolean save = ioWarehouseService.save(ioWarehouse);

            for (CTIoWarehouse ctIoWarehouse : list2) {
                System.out.println(ctIoWarehouse.getIomWarehouses());
                for (CTIomWarehouse iomWarehouse : ctIoWarehouse.getIomWarehouses()) {
                    String id= UUID.randomUUID().toString().replace("-","").substring(0,10);
                    String iomGoods= UUID.randomUUID().toString().replace("-","").substring(0,9);
                    iomWarehouse.setId(id);
                    iomWarehouse.setIomGoodsno(iomGoods);
                    iomWarehouse.setIomTime(ioWarehouse.getIoTime());
                    iomWarehouse.setIomNo(ioWarehouse.getIoNo());

                    System.out.println(iomWarehouse.toString());
                    iomWarehouseService.save(iomWarehouse);
                }
            }
            return new JsonResult(1,"ok","成功");
        }else
        {
            return new JsonResult(0,"no","所出库的商品不存在失败");
        }

    }
}
